package com.xiaoq.push.endpoint;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.push.entity.MessageEntity;
import com.xiaoq.push.entity.TargetUserDTO4ReceiveMsg;
import com.xiaoq.push.service.MiPushServerService;
import com.xiaoq.push.service.PushMessageService;
import com.xiaoq.push.vo.MessageVo;
import com.xiaoq.users.entity.UserEntity;
import com.xiaoq.users.service.UserService;
import com.xiaoq.vo.PagingVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Api(tags={"推送消息相关接口"})
@RestController
@RequestMapping(value = "/api/v1/user")
public class PushMessageEndpoint {
	
	private class MessageListResponse extends GenericListResponse<MessageVo>{}
	
	@Autowired
    PushMessageService pushMessageService;
	
	@Autowired
	private UserService userService;
	
	private static final Logger LOG = LoggerFactory.getLogger(PushMessageEndpoint.class);
	
	@ApiOperation(value = "经营粉丝发消息接口", notes = "经营粉丝发消息接口")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "经营粉丝发消息接口") })
	@PostMapping(value="/{userId}/messages", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	public ResponseEntity<?> createMsgAndSendToUmeng(
			@PathVariable(value = "userId", required = false)@ApiParam(value="userId",required=false)String userId,
			@RequestParam(value="targetIds",required=true)@ApiParam(value="目标的userId集合",required=false)List<String> targetIds,
			@RequestBody MessageVo messageVo) {
		messageVo.setMsgSenderId(userId);
		List<TargetUserDTO4ReceiveMsg> targetUserList = new ArrayList<>();
		for(String targetId:targetIds){
			UserEntity user = userService.getUserByID(targetId);
			String osType = user.getTermDevice().getOsType();
			String regId = user.getTermDevice().getRegId();
			TargetUserDTO4ReceiveMsg targetUser = new TargetUserDTO4ReceiveMsg();
			targetUser.setMainOsType(osType);
			targetUser.setRegId(regId);
			targetUser.setUserId(targetId);
			targetUserList.add(targetUser);
		}
		messageVo.setTargetUserDTOs(targetUserList);
		MessageEntity message = new MessageEntity();
		BeanUtils.copyProperties(messageVo, message);
		message.setTargetUserDTOs(messageVo.getTargetUserDTOs());
		message.setMsgCatalogueCode("1");  // TODO这个版块是让用户选还是直接写死在系统里面
		pushMessageService.createMsgAndSendToXiaomi(message);
		
		return GenericResponse.ok();
		
	}
	
	@ApiOperation(value = "获取某个用户某个目录下的消息列表(分页)", notes = "获取某个用户某个目录下的消息列表(分页)")
	@ApiResponses({ @ApiResponse(code = 200, response = MessageListResponse.class, message = "获取某个用户某个目录下的消息列表(分页)") })
	@GetMapping(value = "/{userId}/messages", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public ResponseEntity<?>findPagingMessagesOfUserByMsgContentType(
			@PathVariable(value = "userId", required = false)@ApiParam(value="userId",required=false)String userId,
            @ApiParam(required = false, name = "msgCatalogueCode", value = "消息分类目录编码")
            @RequestParam(value = "msgCatalogueCode", required = false) Integer msgCatalogueCode,
            @ModelAttribute(value = "pvo") PagingVo pvo) {
				
		List<MessageEntity> messageEntityList = pushMessageService
                .findPagingMessagesOfUserByMsgCatalogueCode(userId, msgCatalogueCode, pvo);
		List<MessageVo> messageVoList = new ArrayList<>();
		MessageVo messageVo = null;
		for(MessageEntity message:messageEntityList){
			messageVo = new MessageVo();
			BeanUtils.copyProperties(message, messageVo);
			messageVoList.add(messageVo);
		}
		Long total = (Long) pvo.getTc();
		return MessageListResponse.listAndCount(messageVoList, total);
		
	}
	
	@ApiOperation(value = "测试发送信息", notes = "测试发送信息")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "测试dubbo发送信息") })
	@PostMapping(value = "/sendMessage", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> sendMessage(
			@RequestParam(value = "userId", required = false)@ApiParam(value="这个是目标的userId并不是自己Token里的",required=false)String userId,
			@RequestParam(value = "catalogueCode") String catalogueCode,
			@RequestParam(value = "msgTitle") String msgTitle, 
			@RequestParam(value = "msgContent")String msgContent,
			@RequestParam(value = "mesTarId")String mesTarId,
			@RequestParam(value = "mesType")String mesType,
			@RequestParam(value = "mesUrl")String mesUrl,
			@RequestParam(value = "cornershopId")String cornershopId,HttpServletRequest request) {
		userId = (String) request.getAttribute("userId");
		LOG.info("发信息的userId为:"+userId);
		pushMessageService.sendMessageToXiaomi(userId, catalogueCode, msgTitle, msgContent, mesTarId, mesType, cornershopId,mesUrl,true);
		return GenericResponse.ok();

	}
	

}
